<template>
  <div class="app-container">
    <!-- 搜索表单 -->
    <el-form
      ref="queryRef"
      :inline="true"
      label-width="68px"
      :model="queryParams"
      v-show="showSearch"
    >
      <el-form-item label="机柜编号" prop="cabinetNo">
        <el-input
          v-model="queryParams.cabinetNo"
          placeholder="请输入机柜编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分类" prop="cabinetTypeId">
        <el-select
          v-model="queryParams.cabinetTypeId"
          class="m-2"
          placeholder="选择分类"
          size="small"
          style="width: 100%"
        >
          <el-option
            v-for="item in cabinetTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 功能按钮栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          @click="handleDelete"
          :disabled="multiple"
          >删除</el-button
        >
      </el-col>
      <right-toolbar></right-toolbar>
    </el-row>

    <!-- 数据展示表格 -->
    <el-table v-loading="loading" :data="cabinetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="机柜编号" prop="cabinetNo" />
      <el-table-column label="名称" prop="name" />
      <el-table-column label="分类" prop="cabinetTypeName" />
      <el-table-column label="总插槽数" prop="totalSlots" />
      <el-table-column label="空闲插槽数" prop="freeSlots" />
      <el-table-column label="可用数量" prop="usedSlots" />
      <el-table-column label="描述" prop="description" />
      <el-table-column label="状态" prop="status" width="120">
        <template #default="scope">
          {{
            scope.row.status == "0"
              ? "未投放"
              : scope.row.status == "1"
              ? "使用中"
              : "故障"
          }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button link type="primary" icon="Info" @click="handleInfo(scope.row.id)">详情</el-button>
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改充电柜机对话框 -->
    <el-dialog :title="title" v-model="open" width="600px" append-to-body>
      <el-form
        ref="cabinetRef"
        :model="form"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="编号" prop="cabinetNo">
          <el-input v-model="form.cabinetNo" placeholder="请输入柜机编号" />
        </el-form-item>
        <el-form-item label="分类" prop="cabinetTypeId">
          <el-select
            v-model="form.cabinetTypeId"
            class="m-2"
            placeholder="分类"
          >
            <el-option
              v-for="item in cabinetTypeList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="form.description"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>


    <!-- 柜机详细信息 -->
    <el-dialog title="柜机详细信息" v-model="openInfo" width="50%" append-to-body>
      <el-descriptions title="柜机信息">
        <el-descriptions-item label="机柜编号">{{ cabinet.cabinetNo }}</el-descriptions-item>
        <el-descriptions-item label="机柜名称">{{ cabinet.name }}</el-descriptions-item>
        <el-descriptions-item label="总插槽数量">{{ cabinet.totalSlots }}</el-descriptions-item>
        <el-descriptions-item label="空闲插槽量">{{ cabinet.freeSlots }}</el-descriptions-item>
        <el-descriptions-item label="已使用插槽">{{ cabinet.usedSlots }}</el-descriptions-item>
        <el-descriptions-item label="可用数量">{{ cabinet.availableNum }}</el-descriptions-item>
        <el-descriptions-item label="描述">{{ cabinet.description }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ cabinet.createTime }}</el-descriptions-item>
      </el-descriptions>
      <el-descriptions title="柜机插槽信息"></el-descriptions>
      <el-table :data="cabinetSlotList" style="width: 100%;">
        <el-table-column label="插槽编号" prop="slotNo" width="120"/>
        <el-table-column label="使用状态" prop="status" width="100">
          <template #default="scope">
            {{ scope.row.status == '1' ? '占用' : '空闲' }}
          </template>
        </el-table-column>
        <el-table-column label="充电宝标号" prop="powerBank.powerBankNo" width="130"/>
        <el-table-column label="充电宝电量(%)" prop="powerBank.electricity" width="130"/>
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancelInfo">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="PowerBank">
import {
  listCabinet,
  addCabinet,
  getCabinet,
  updateCabinet,
  delCabinet,
  getAllInfo,
} from "@/api/device/cabinet";

import { getCabinetTypeList } from "@/api/device/cabinetType";

import { reactive, toRef } from "vue";
import { ElMessageBox } from "element-plus";

const { proxy } = getCurrentInstance();

//定义分页列表数据模型
const cabinetList = ref([]);

//定义列表总记录数模型
const total = ref(0);
//加载数据时显示的动效控制模型
const loading = ref(false);
//定义隐藏搜索控制模型
const showSearch = ref(true);

//添加/修改参数
const title = ref("");
const open = ref(false);

const data = reactive({
  //定义搜索模型
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    powerBankNo: null,
    name: null,
    cabinetTypeId: null,
    totalSlots: null,
    availableSlots: null,
    description: null,
    locationId: null,
    status: null,
  },
  form: {},
  rules: {
    cabinetTypeId: [
      { required: true, message: "分类必须选择", trigger: "blur" },
    ],
    name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    cabinetNo: [
      { required: true, message: "柜机编号不能为空", trigger: "blur" },
    ],
  },
});

const { queryParams, form, rules } = toRefs(data);

//分页列表
function getList() {
  loading.value = true;
  listCabinet(queryParams.value).then((response) => {
    cabinetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

//柜机类型列表
const cabinetTypeList = ref([]);
function fetchCabinetTypeList() {
  loading.value = true;
  getCabinetTypeList().then((response) => {
    cabinetTypeList.value = response.rows;
    loading.value = false;
  });
}

//搜索按钮
function handleQuery() {
  getList();
}
//重置按钮
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

//新增按钮操作
function handleAdd() {
  title.value = "添加充电柜机";
  open.value = true;
  reset();
}

//修改按钮操作
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  open.value = true;
  getCabinet(_id).then((response) => {
    form.value = response.data;
    title.value = "修改充电柜机";
  });
}

//添加提交按钮
function submitForm() {
  proxy.$refs["cabinetRef"].validate((valid) => {
    if (valid) {
      if (form.value.id) {
        updateCabinet(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addCabinet(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

//删除按钮
//批量删除
const ids = ref([]);
//定义单选控制模型
const single = ref(true);
//定义多选控制模型
const multiple = ref(true);
function handleDelete(row) {
  const _id = row.id || ids.value;
  ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      return delCabinet(_id);
    })
    .then(() => {
      proxy.$modal.msgSuccess("删除成功");
      getList();
    })
    .catch(() => {});
}

//多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}


//查看详情
const openInfo = ref(false);
const cabinetSlotList = ref([]);
const cabinet = ref({});
function handleInfo(id) {
  getAllInfo(id).then(response => {
    cabinet.value = response.data.cabinet;
    cabinetSlotList.value = response.data.cabinetSlotList;
    openInfo.value = true;
  });
}



// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 详情取消按钮
function cancelInfo() {
  openInfo.value = false;
   
}

//表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    cabinetNo: null,
    cabinetTypeId: null,
    totalSlots: null,
    availableSlots: null,
    description: null,
    locationId: null,
    status: null,
    delFlag: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
  };
  proxy.resetForm("cabinetRef");
}

getList();
fetchCabinetTypeList();
</script>
